package org.dragon.mappers;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.dragon.models.Permission;

import java.util.List;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/5/22 9:42
 */
@Mapper
public interface PermissionExtendMapper extends PermissionMapper {

    @Select("select * from permission where code = #{code}")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "code", property = "code", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Permission findByCode(@Param("code") String code);

    @Select("select * from privilege_permission pp left join permission p on pp.permission_id = p.id where pp.privilege_id = #{id}")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Permission> findByPrivilegeId(@Param("id") Long id);

    @Delete({
            "delete from privilege_permission",
            "where permission_id = #{id,jdbcType=BIGINT}"
    })
    int deletePrivilegePermission(@Param("id") Long id);

    @Delete({
            "delete from role_permission",
            "where permission_id = #{id,jdbcType=BIGINT}"
    })
    int deleteRolePermission(@Param("id") Long id);

    @Update("truncate permission")
    void clear();

    @Update("truncate privilege_permission")
    void clearPrivilegePermission();

    @Update("truncate role_permission")
    void clearRolePermission();
}
